<?php

namespace App\Http\Controllers\Client\Activity\Talent;

use App\Http\Controllers\ClientController;
use App\Models\Tools\Data;
use Illuminate\Support\Facades\DB;

class ActivityListBeforeController extends ClientController
{
    // 霸王餐活动参与前的检查
    public function index()
    {
        $all = $this->all;

        $errMsg = '';
        // 查询达人地址
        $talent = DB::table('talent')->where('we_id', '=', $this->we_id)->first();
        $talent = json_decode(json_encode($talent), true);
        if (is_null($talent)) {
            $errMsg = '您未认证来飞网信息，无法参与霸王餐';
            $city = substr($all['adcode'],0,4).'00';
            $talent = [
                'city' => $city,
                'city_name' => DB::table('dict_area')->where('id','=',$city)->value('name'),
            ];
            if (!in_array($talent['city'], Data::$lotteryPermitCity)) {
                $errMsg = '很抱歉，您所在的城市尚未开通霸王餐活动';
            }
        } else {
            $talent['city_name'] = DB::table('dict_area')->where('id', '=', $talent['city'])->value('name');
            $city = $talent['city'];
            if (is_null($talent['city'])) {
                $errMsg = '您未设置常住地信息,请在“我的”页面设置您的常住地区';
            } elseif ($talent['status'] !== Data::DATA_DB_TALENT_STATUS_SUCCESS) {
                $errMsg = '您的账号未认证通过，暂时无法参与霸王餐';
            } elseif (!in_array($talent['city'], Data::$lotteryPermitCity)) {
                $errMsg = '很抱歉，您所选择的常住城市尚未开通霸王餐活动';
            }
        }

        $type = [];
        foreach (Data::$laifeiShopTypes as $key => $value) {
            $type[] = ['text' => $value, 'value' => $key];
        }

        $districts = DB::table('dict_area')
            ->where('pid', '=', $city)
            ->select('id as value', 'name as text')
            ->get()
            ->toArray();

        return $this->setReturnJson(0, $errMsg, ['talent' => $talent, 'type' => $type, 'districts' => $districts]);


    }

}
